package cn.com.ghy.android.widget.way;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.MyWayBean;
import cn.com.ghy.android.bean.OrderDetailBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.CustOpeService;
import cn.com.ghy.android.service.LineOpeService;
import cn.com.ghy.android.service.OrderOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;
import cn.com.ghy.android.widget.chat.CustomerDetailInfoList;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:我的路线模块
 * @author wuz  @date 2011-5-23 14:20:15
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class MyWay extends Activity {

	private Spinner spiMyWay = null;
	private Button btnSelect = null;
	private ListView listView = null;
	private ProgressDialog progressDialog = null;
	private Bundle bundle = null;
	private Intent intent = null;

	private CustomerInfoListAdapter customerInfoListAdapter = null;

	private LoginInfoBean loginInfo = null;
	private String wayid = "";
	private CustomerInfoBean customerInfo = null;
	private List<OrderDetailBean> listOrderDetail = null;
	private List<MyWayBean> myWayList = null;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.my_way);
		getSession();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
		myWayList = (List<MyWayBean>) this.getIntent().getSerializableExtra("myWayList");
	}

	private void findView() {
		spiMyWay = (Spinner) this.findViewById(R.id.spiMyWay);
		btnSelect = (Button) this.findViewById(R.id.btnSelect);
		listView = (ListView) this.findViewById(R.id.listCustomerInfoList);
	}

	private void init() {
		ArrayAdapter<MyWayBean> myWayAdapter = new ArrayAdapter<MyWayBean>(MyWay.this,
				android.R.layout.simple_spinner_item, myWayList);
		myWayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiMyWay.setAdapter(myWayAdapter);
	}

	private void addListener() {
		btnSelect.setOnClickListener(btnListener);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CustomerInfoBean bean = (CustomerInfoBean) parent.getItemAtPosition(position);
				showDialog(MyWay.this, bean);
			}

		});
		spiMyWay.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				wayid = ((MyWayBean) parent.getItemAtPosition(position)).getWayid();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});
	}

	//按钮监听
	private View.OnClickListener btnListener = new View.OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnSelect :
					new CustomerInfoAsyncTask().doStart();
					break;
			}
		}
	};

	private class CustomerInfoListAdapter extends BaseAdapter {

		private Context context = null;
		private List<CustomerInfoBean> list = null;

		public CustomerInfoListAdapter(Context context, List<CustomerInfoBean> list) {
			this.context = context;
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public CustomerInfoBean getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;//充气者
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.my_way_lay,
					null);
			CustomerInfoBean bean = list.get(position);
			TextView tv = (TextView) linearLayout.findViewById(R.id.labTxtCustomerId);
			tv.setText(bean.getPartner());
			tv = (TextView) linearLayout.findViewById(R.id.labTxtCustomerName);
			tv.setText(bean.getName_org1());
			return linearLayout;
		}

	}

	@SuppressWarnings("rawtypes")
	private class CustomerInfoAsyncTask extends AsyncTask {

		private List<CustomerInfoBean> list = null;

		@SuppressWarnings("unchecked")
		public void doStart() {
			progressDialog = new ProgressDialog(MyWay.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("选择成功！正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.execute();
		}

		@Override
		protected Object doInBackground(Object... params) {//表示此处接受的参数为0到多个Object类型的对象，或者是一个Object[]
			try {
				LineOpeService service = new LineOpeService();
				//list = service.LineDetail(loginInfo, wayid);//返回List<CustomerInfoBean>
				CustomerInfoBean customerInfoBean = new CustomerInfoBean();
				customerInfoBean.setCapacity("capacity");
				customerInfoBean.setCityname("苏州");
				customerInfoBean.setCk1name("ck1name");
				customerInfoBean.setName_org1("name_org1");//客户名称
				customerInfoBean.setJxsname("jxsname");
				customerInfoBean.setZonename("zonename");
				customerInfoBean.setPartner("partner");
				list = new ArrayList<CustomerInfoBean>();
				list.add(customerInfoBean);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
			progressDialog.dismiss();
			listView.post(new Runnable() {
				public void run() {
					customerInfoListAdapter = new CustomerInfoListAdapter(MyWay.this, list);
					listView.setAdapter(customerInfoListAdapter);
				}
			});
			return list;
		}
	}

	private void showDialog(Context context, final CustomerInfoBean customerInfo) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示信息");
		builder.setMessage("名称:" + customerInfo.getName_org1() + "\n编号:"
				+ customerInfo.getPartner());
		builder.setPositiveButton("查看客户", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				httpThread = new HttpThread(handler);
				httpThread.doStart(customerInfo.getPartner());
			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}

	private class HttpThread extends Thread {

		private Handler handle = null;
		private String partner = null;
		private String intentflag = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		private void doStart(String partner) {
			this.partner = partner;
			progressDialog = new ProgressDialog(MyWay.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			try {
				CustOpeService service = new CustOpeService();
				List<CustomerInfoBean> list = service.GetCustShow(partner);
				customerInfo = list.get(0);
				OrderOpeService orderOpeService = new OrderOpeService();
				listOrderDetail = orderOpeService.GetLastMatnr(partner);
				progressDialog.dismiss();
				Message message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;//给message设置what
				bundle.putSerializable("customerInfo", customerInfo);
				bundle.putString("intentflag", intentflag);
				bundle.putSerializable("listOrderDetail", (Serializable) listOrderDetail);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {

		@SuppressWarnings("unchecked")
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					customerInfo = (CustomerInfoBean) m.getData().getSerializable("customerInfo");
					
					listOrderDetail = (List<OrderDetailBean>) m.getData().getSerializable(
							"listOrderDetail");
					bundle = new Bundle();
					bundle.putSerializable("customerInfo", customerInfo);
					bundle.putSerializable("listOrderDetail", (Serializable) listOrderDetail);
					intent = new Intent(MyWay.this, CustomerDetailInfoList.class);
					intent.putExtras(bundle);
					MyWay.this.startActivity(intent);
					break;
			}
		}
	};
}
